package com.hj.dapp.business.oss.controller;

import com.hj.dapp.business.oss.model.FileUpLoadDto;
import com.hj.dapp.business.oss.model.FileUploadVo;
import com.hj.dapp.business.oss.service.OssService;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;

import static com.hj.dapp.common.constant.OssConstant.TYPE_AVATAR;

/**
 * @author 胡奥程
 * @version 1.0
 * @date 2024/1/2
 */
@RequestMapping("/oss")
@RestController
public class OssController {
    @Autowired
    private OssService ossService;
    @PutMapping("/fileUpload")
    public FileUploadVo fileUpload(@RequestBody FileUpLoadDto fileUpLoadDto) {
        return ossService.fileUpload(fileUpLoadDto);
    }

    @PostMapping("/avatarUpload")
    public FileUploadVo avatarUpload(MultipartFile file) {
        try {
            FileUpLoadDto fileUpLoadDto = new FileUpLoadDto();
            InputStream inputStream = file.getInputStream();
            fileUpLoadDto.setExtName(FilenameUtils.getExtension(file.getOriginalFilename()));
            fileUpLoadDto.setInputStream(inputStream);
            fileUpLoadDto.setType(TYPE_AVATAR);
            return ossService.fileUpload(fileUpLoadDto);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
